package com.mt.fpb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.fpb.enums.ResponseCode;
import com.mt.fpb.mapper.RxEmployeeMapper;
import com.mt.fpb.mapper.RxEmployeeWxUserMapper;
import com.mt.fpb.mapper.UserMapper;
import com.mt.fpb.mapper.UserWxUserMapper;
import com.mt.fpb.model.RxEmployee;
import com.mt.fpb.model.RxEmployeeWxUser;
import com.mt.fpb.model.User;
import com.mt.fpb.model.UserWxUser;
import com.mt.fpb.model.vo.CommonResult;
import com.mt.fpb.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yu
 * @date 2021年08月10日 14:07
 */
@RestController
@RequestMapping("ruixiang/small")
public class SmallController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RxEmployeeMapper rxEmployeeMapper;

    @Autowired
    private RxEmployeeWxUserMapper rxEmployeeWxUserMapper;

    /**
     * 小金库内部员工以及客户登录
     * @Author yu
     * @date 2021/8/10 14:31
     * @Param [phoneNumber, openId]
     * @return com.mt.fpb.model.vo.CommonResult
     **/

    @GetMapping("login")
    public CommonResult login(String openId){
        //通过openId查询员工表，有数据直接进入我的资产页面，否则跳到绑定页面
        QueryWrapper<RxEmployee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id",openId);
        RxEmployee rxEmployee = rxEmployeeMapper.selectOne(queryWrapper);
        if(rxEmployee!=null){
            //不为空拿出员工的信息
            Integer employeeId =rxEmployee.getEmployeeId();
            String phoneNumber = rxEmployee.getPhoneNumber();
            Map<String,Object> map = new HashMap<>();
            map.put("employeeId",employeeId);
            map.put("phoneNumber",phoneNumber);
            return CommonResult.success(map);
        }else{
            System.out.println("员工信息为空");
            return CommonResult.fail(500,"该用户未认证");
        }

    }

    @GetMapping("login1")
    public CommonResult login1(String phoneNumber, String openId, String code){
        System.out.println("手机号"+phoneNumber);
        System.out.println("openId:"+openId);
        System.out.println("前台code:"+code);

        //1 验证code
        String msCode = (String) redisUtil.get(phoneNumber);
        System.out.println("后台redis存储的msCode:" + msCode);
        if (code == null && "".equals(code) && msCode == null && "".equals(msCode)) {
            System.out.println("短信验证码code获取失败");
            return CommonResult.fail(500, "验证码错误");
        }
        if (!msCode.equals(code)) {
            System.out.println("短信验证码匹配验证失败");
            return CommonResult.fail(500, "验证码错误");
        }

        //设置查询条件
        QueryWrapper<RxEmployee> queryWrapper = new QueryWrapper<RxEmployee>();
        //通过手机号进行查询
        queryWrapper.eq("phone_number",phoneNumber);
        //得到查询结果
        RxEmployee e = rxEmployeeMapper.selectOne(queryWrapper);
        System.out.println("e:"+e);
        if(e != null){

            //建立员工与微信用户的绑定
            RxEmployeeWxUser rxEmployeeWxUser = new RxEmployeeWxUser();
            rxEmployeeWxUser.setEmployeeId(e.getEmployeeId());
            rxEmployeeWxUser.setOpenId(openId);
            //将员工id和openId插入到员工-微信用户关系表中
            rxEmployeeWxUserMapper.insert(rxEmployeeWxUser);

            //返回查询出来的员工信息
            return CommonResult.success(e.getEmployeeId());

        }else{
            //返回提示信息
            return  CommonResult.fail(ResponseCode.UNAUTH);
        }

    }

    /**
     * 小金库的登出接口
     * @Author yu
     * @date 2021/8/10 16:07
     * @Param [employeeId, openId]
     * @return com.mt.fpb.model.vo.CommonResult
     **/

    @GetMapping("logout")
    public CommonResult logout(String employeeId,String openId){

        QueryWrapper<RxEmployeeWxUser> queryWrapper = new QueryWrapper<RxEmployeeWxUser>();

        System.out.println(employeeId  +"  "+openId);
        int result = rxEmployeeWxUserMapper.delete(queryWrapper);

        if(result>0){
            return CommonResult.success(ResponseCode.LOGOUT);
        }
        return CommonResult.fail(ResponseCode.FAILED);
    }


}
